//
//  BaitController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/21.
//  Copyright © 2015年 game. All rights reserved.
//

#import "BaitController.h"
#import "NewsCell.h"
#import "CharacterCell.h"
#import "UIImageView+WebCache.h"

#define PI 3.14159265358979323846264338327950288


@interface BaitController ()<UITableViewDataSource,UITableViewDelegate>
{

    UITableView *_tableview;//显示视屏列表分类
    CGFloat _cellHeight;
    CGFloat _CharacCellHeight;
    UITableView *_ClassifyTableview;//分类tableview
    
    UIImageView *_downimg;
    BOOL isClick;
}
@end

@implementation BaitController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self CrectTableview];
}
-(void)CrectTableview{
    _tableview = [self CreatTableview:_tableview WhithRect:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-44) AndStyle:UITableViewStylePlain];
    _tableview.tag = 1;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    [_tableview registerClass:[NewsCell class] forCellReuseIdentifier:@"cell"];
    [self TableviewHead];
    
    
    _ClassifyTableview =[[UITableView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH,0) style:UITableViewStylePlain];
    _ClassifyTableview.tag = 2;
    _ClassifyTableview.dataSource = self;
    _ClassifyTableview.delegate = self;
    [_ClassifyTableview registerClass:[CharacterCell class] forCellReuseIdentifier:@"Characell"];
    [self.view addSubview:_ClassifyTableview];
}
-(void)TableviewHead{
    /*
     tableview的头 设置高度为40，底层为Bnt
     */
    UIButton *bnt = [UIButton buttonWithType:UIButtonTypeSystem];
    [bnt addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
    
    bnt.frame = CGRectMake(0, 0, SCREEN_WIDTH,40);
    bnt.backgroundColor = RGBA(242, 242, 242, 1);
    
    UILabel *label = [self CrectLabelWhithRect:CGRectMake(10, 0, 100, H(bnt))];
    label.text = @"鱼饵";
    [bnt addSubview:label];
    
    //右边箭头图片
    _downimg = [[UIImageView alloc]initWithFrame:CGRectMake(W(bnt)-40, 10, 20, 20)];
    _downimg.image = [UIImage imageNamed:@"btn-down"];
    [bnt addSubview:_downimg];
    
    _tableview.tableHeaderView = bnt;
}

-(void)Click:(UIButton *)bnt{
    
    isClick = !isClick;
    if (isClick) {
        
#pragma mark--动画执行
        [UIView animateWithDuration:0.3 animations:^{
            [self makeRotation:_downimg speedX:0 speedY:0.5];
            
            _ClassifyTableview.frame = CGRectMake(0, 40, SCREEN_WIDTH,SCREEN_HEIGHT-64-44-40);
        }];
        
    }else{
        
        
        [UIView animateWithDuration:0.3 animations:^{
            [self makeRotation:_downimg speedX:0 speedY:-0.5];
            
            _ClassifyTableview.frame = CGRectMake(0, 40, SCREEN_WIDTH,0);
        }];
        
    }
    
}
#pragma mark---图片旋转
-(UIImageView *) makeRotation:(UIImageView *)image speedX:(float)X speedY:(float)Y
{
    
    //    #define PI 3.14159265358979323846264338327950288
    if (Y<0) {
        image.transform = CGAffineTransformMakeRotation(atan(X/(-Y)));
    }else if(Y>0){
        image.transform = CGAffineTransformMakeRotation(atanf(X/(-Y))-PI);
    }
    return image;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (tableView.tag== 1) {
        return 0;
    }else{
        return 30;
    }
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (tableView.tag==1) {
        return 1;
    }else{
        
        return 10;
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    /*头部高度为30*/
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 7.5, 4, 15)];
    imgview.backgroundColor = mainColor;
    radius(imgview, 2);
    [view addSubview:imgview];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 100, H(view))];
    lable.text = @"按渔种";
    [view addSubview:lable];
    
    return view;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag==1) {
        return 15;
    }else{
        return 1;
        
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==1) {
        return _cellHeight;
    }else{
        return _CharacCellHeight;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag==1) {
        
        
        NewsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell= [[NewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        
        cell.image.backgroundColor = [UIColor clearColor];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:@"http://pic.58pic.com/58pic/15/25/48/94m58PIC5R6_1024.jpg"]];
        
        
        cell.title.text = @"信而海钓黑豹鱼的钩点选择和钩组搭配技巧";
        cell.time.text = @"2015-12-12";
        cell.clikNumb.text =[NSString stringWithFormat:@"%d次点击",37495] ;
        _cellHeight = cell.cellHight;
        
        return cell;
    }else{
        CharacterCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        
        if (!cell) {
            cell = [[CharacterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Characell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell CrectBnt];
        _CharacCellHeight = cell.cellHight;
#pragma mark---实现跳转，用Block实现
        cell.ButtionBlock = ^(UIButton *sender){
            //                NSLog(@"--%ld",indexPath.section);
            //
            //                NSLog(@"--%ld",tag);
            
            NSLog(@"xixi:%ld",indexPath.section+sender.tag);
            
        };
        return cell;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
